package cn.dkdoo.utils.config;

import cn.dkdoo.utils.annotation.EnableDkdTransactional;
import cn.dkdoo.utils.controller.ComRollbackController;
import cn.dkdoo.utils.utils.DkdSpringUtils;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Nicky.Tang on 2021/5/10 10:55 下午
 *
 * @since 02.12.10
 */
public class TransactionManagementConfigurationSelector implements ImportSelector {

    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        Map<String, Object> enableDkdTransactional = annotationMetadata.getAnnotationAttributes(EnableDkdTransactional.class.getName());
        boolean openTra = Boolean.valueOf(enableDkdTransactional.get("openTra").toString());
        if (!openTra) {
            return new String[0];
        }

        List<String> res = new ArrayList<>();
        boolean openRolApi = Boolean.valueOf(enableDkdTransactional.get("openRolApi").toString());
        if (openRolApi) {
            res.add(ComRollbackController.class.getName());
        }
        //res.add(DkdTransactionConfig.class.getName());
        res.add(DkdTransactionalAspect.class.getName());
        res.add(DkdCheckTraOpenAspect.class.getName());
        res.add(DkdSpringUtils.class.getName());

        return res.toArray(new String[res.size()]);
    }
}
